package com.example.jh352160.surfingtv.mypage.childPage;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.guid.GuidActivity;
import com.example.jh352160.surfingtv.mypage.MypageMain;
import com.example.jh352160.surfingtv.mypage.customView.LoginInfomation;
import com.example.jh352160.surfingtv.network.item.LoginItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.example.jh352160.surfingtv.guid.GuidActivity.page;

/**
 * Created by jh352160 on 2016/7/27
 */

public class LoginPage extends AppCompatActivity {

    ImageView closeImage;
    Button loginButton;
    EditText username, password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_login);
        closeImage = (ImageView) findViewById(R.id.close_image);
        loginButton = (Button) findViewById(R.id.login_button);
        username = (EditText) findViewById(R.id.usernameEdit);
        password = (EditText) findViewById(R.id.passwordEdit);

        closeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginPage.this.finish();
                page = 1;
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //api
                Retrofit retrofit = CommonRetrofit.getRetrofit();
                LoginService loginService = retrofit.create(LoginService.class);
                LoginInfomation loginInfomation = new LoginInfomation();
                loginInfomation.setUsername(username.getText().toString());
                loginInfomation.setPassword(password.getText().toString());
                Call<LoginItem> call = loginService.login(loginInfomation);
                call.enqueue(new Callback<LoginItem>() {
                    @Override
                    public void onResponse(Call<LoginItem> call, Response<LoginItem> response) {
                        if (response.body().getCode().equals("200")) {
                            GuidActivity.isLogin = true;
                            GuidActivity.page = 4;
                            MypageMain.setUserData(response.body().getData());
                            LoginPage.this.finish();
                            SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                            editor.putBoolean("isLogin", true);
                            editor.putString("token", response.body().getData().getToken());
                            editor.apply();
                        } else {
                            Toast.makeText(LoginPage.this, response.body().getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginItem> call, Throwable t) {
                        Log.e("Retrofit Error", t.toString());
                    }
                });
            }
        });
    }
}
